#!/usr/bin/env python
#
# Copyright (C) 2011 Clement Lorteau <clem@lorteau.fr>	
#		
# This file is part of QuickScreenShots.		
# 	
# This program is free software; you can redistribute it and/or modify	
# it under the terms of the GNU General Public License as published by		
# the Free Software Foundation; version 3.	
# 	
# This program is distributed in the hope that it will be useful,		
# but WITHOUT ANY WARRANTY; without even the implied warranty of		
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the		
# GNU General Public License for more details.		
# 		
# You should have received a copy of the GNU General Public License		
# along with this program; if not, write to the Free Software		
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

import cPickle as pickle
import platform
import os

if platform.system() == 'Windows':
	save_dir = os.path.join(os.getenv('APPDATA'), 'QuickScreenShots')
elif platform.system() == 'Darwin':
	save_dir = os.path.join(os.getenv('HOME'), 'Library', 'Application Support',
							'QuickScreenShots')
else:
	raise Exception('Unrecognized platform')
save_file = 'config'

class Option(object):
    def __init__(self, default, description, value=None):
        self.default = default
        self.value = value
        self.description = description
        
    def SetValue(self, value):
        self.value = value
    
    def GetValue(self):
        """Returns the value set for this option, or the default value if none was set"""
        if self.value != None:
            return self.value
        else:
            return self.default
    
    def GetDefault(self):
        return self.default
    
    def GetDescription(self):
        return self.description
        
    def Reset(self):
        """Sets the value back to the option's default"""
        self.value = self.default

            
class Options(object):
        
    def __init__(self):
        self.win_start = Option(True, 'Start with Windows')
        self.start_minimized = Option(False, 'Start minimized')
        self.check_updates = Option(True, 'Check for udpates once a week')
        self.sound_use = Option(True, 'Play sound on screenshot')
        self.include_cursor = Option(True, 'Include mouse cursor in screenshots')
        self.shadow_use = Option(False, 'Add a shadow around screenshot')
        self.shadow_background = Option('#FFFFFF', 'Background of the screenshot when adding a shadow')
        self.shadow_color = Option('#D0D0D0', 'Color of the shadow')
        self.rotate_use = Option(False, 'Rotate the screenshot')
        self.rotate_angle = Option(10, 'Angle of rotation in degrees')
        self.rotate_background = Option('#FFFFFF', 'Background of the screenshot after rotation')
        self.save_use = Option(False, 'Automatically save screenshots')
        if platform.system() == 'Windows':
        	self.save_dir = Option(os.path.join(os.getenv('TMP'), 'screenshots'), 'Where to save the screenshots')
        elif platform.system() == 'Darwin':
        	self.save_dir = Option(os.path.join('tmp', 'screenshots'), 'Where to save the screenshots')
        else:
        	raise Exception('Platform unrecognized')
        self.save_file = Option('screenshot', 'Base file name of saved screenshots')
        self.copy_use = Option(True, 'Automatically copy the screenshots to clipboard')
        self.delay = Option(0, 'Delay in seconds before taking screenshot')
        self.mono_use = Option(False, 'Take black & white screenshots')
        self.kb_desktop = Option('Ctrl+Shift+D', 'Desktop screenshot keyboard shortcut')
        self.kb_window = Option('Ctrl+Shift+W', 'Active window screenshot keyboard shortcut')
        self.kb_region = Option('Ctrl+Shift+R', 'Region screenshot keyboard shortcut')
        self.draw_line_color = Option('#FF0000', 'Draw line color')
        self.draw_line_size = Option(3, 'Draw line thickness in pixels')

    def Save(self):
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        file = open(os.path.join(save_dir, save_file), 'w')
        pickle.dump(self, file)
        file.close()

def load():
    file = open(os.path.join(save_dir, save_file), 'r')
    options = pickle.load(file)
    file.close()
    return options

if __name__ == '__main__':
    options = Options()
    options.shadow_use.SetValue(True)
    options.save_use.SetValue(True)
    options.save_dir.SetValue('d:\\temp')
    options.Save()
    options_new = load()
    print options_new.save_use.GetValue()
    print options_new.save_dir.GetValue()